package start.doubudouyin.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import start.doubudouyin.entity.join.VideoPlay;
import start.doubudouyin.entity.Videocomment;
import start.doubudouyin.service.*;
import start.doubudouyin.utils.Result;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/interaction")
@CrossOrigin(origins = "http://localhost:5173/")
public class interactionController {

    @Resource
    private VideocommentService videocommentService;
    @Resource
    private VideolikeService videolikeService;
    @Resource
    private FollowService followService;
    @Resource
    private UserspreferService userspreferService;

    /**
     * 添加评论
     */
    @PutMapping("/addcomment")
    public Result addcomment(@RequestParam("videoId") String videoId,
                             @RequestParam("userId") String userId,
                             @RequestParam("content") String content) {
        //创建一个评论对象
        Videocomment videocomment = new Videocomment();
        videocomment.setVideoId(videoId);
        videocomment.setUserId(userId);
        videocomment.setContent(content);
        Date date = new Date();
        videocomment.setCommentTime(date);
        //添加评论
        if (videocommentService.insert(videocomment)&&videocommentService.recordComment(userId,videoId)>0) {
            return Result.success(); //添加成功
        }
        return Result.error("300", "评论失败");
    }


    @GetMapping("/getComment")
    public Result getComment(@RequestParam("videoId") String videoId) {
        //根据视频id查询评论
        List<Videocomment> list = videocommentService.getComment(videoId);
        return Result.success(list);
    }

    //视频点赞
    @PutMapping("/toggleLike")
    public Result toggleLike(@RequestParam("userId") String userId, @RequestParam("videoId") String videoId ){
        if(!videolikeService.isLiked(userId, videoId)){
            if(videolikeService.addLike(userId,videoId)&&videolikeService.recordLike(userId,videoId)>0)
                return Result.success();
        }else{
            if(videolikeService.deleteLike(userId,videoId)>0&&videolikeService.delRecordLike(userId,videoId)>0)
                return Result.success();
        }
        return Result.error("300","操作失败");
    }
    //关注视频作者
    @PutMapping("/toggleFollowed")
    public Result toggleFollowed(@RequestParam("userId") String userId, @RequestParam("followedId") String followedId ){
        if(!followService.isFollowed(userId, followedId)){
            if(followService.insert(userId,followedId))
                return Result.success();
        }else{
            if(followService.removeFollowed(userId,followedId)>0)
                return Result.success();
        }
        return Result.error("300","操作失败");
    }

    @PutMapping("/toggleShare")
    public Result toggleShare(@RequestParam("userId") String userId, @RequestParam("videoId") String videoId ){
        if(userspreferService.recordShare(userId,videoId)>0){
            return Result.success();
        }else{
            return Result.error("300","分享数据出错");
        }
    }


}
